﻿/*----------------------------------------------------------------
 * 
 * 版权所有：Copyright (C) 2012 
 * 
 * 文件名：MediaController.cs
 * 文件功能描述：
 * 
 * 
 * 作 者：zhangq
 * 邮 箱：zhangqiang@bamabest.com
 * 日 期：2013-04-15 11:11:12
 * 
 * 
 * 修改标识： Author
 * 修改描述： 相关描述
 * 
 * ---------------------------------------------------------------- */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using Common.Extensions.OrderBy;
using Common.Extensions.Where;
using DomainModel;
using EmitMapper;
using IManager;
using UI.JsonModel;
using WebPlatform.FiltersAndSession;
using WebPlatform.Models;

namespace WebPlatform.Controllers
{

    /// <summary>
    /// 
    /// </summary>
    public class MediaController : Controller
    {

        public IManagerMedia IMedia { get; set; }


        public Sessions Sessions { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public ActionResult Index()
        {
            return View();
        }


        /// <summary>
        /// 企业相册
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Index(int page = 1, int pageSize = 15)
        {
            var user = Sessions.User;
            GridData<MediaVM> data = new GridData<MediaVM>();
            var where = PredicateExtension.True<MediaInfo>();
            where = where.And(u => u.CompanyId == user.CompanyId);
            var count = IMedia.LoadCount(where);
            var list = IMedia.LoadAll(where, page, count,
                new OrderByExpression<MediaInfo, DateTime>(u => u.UploadDate, true));


            var mapper = ObjectMapperManager.DefaultInstance.GetMapper<MediaInfo, MediaVM>();

            data.Rows = mapper.MapEnum(list).ToList();
            data.Total = count;
            return Json(data);
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        public ActionResult Add(int aid = 0)
        {
            if (aid > 0)
            {
                var mediainfo = IMedia.GetMedia(aid);
                var mapper = ObjectMapperManager.DefaultInstance.GetMapper<MediaInfo, MediaVM>();
                var mediaVM = mapper.Map(mediainfo);
                return View(mediaVM);
            }
            else
            {
                return View(new MediaVM() { UploadDate = DateTime.Now  });
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Add(MediaVM media)
        {
            bool flag = false;
            if (ModelState.IsValid)
            {
                var mapper = ObjectMapperManager.DefaultInstance.GetMapper<MediaVM, MediaInfo>();

                var mediaInfo = mapper.Map(media);
                mediaInfo.CompanyId = Sessions.User.CompanyId;
                if (!string.IsNullOrEmpty(media.MediaHtml))
                {
                    string pat = @"<embed\s+[^>]*\s*src\s*=\s*([']?)(?<url>\S+)'?[^>]*>";

                    Regex reg = new Regex(pat, RegexOptions.IgnoreCase);
                    Match m = reg.Match(media.MediaHtml);
                    if (m.Success)
                    {
                        mediaInfo.MediaUrl = m.Groups[2].Value.Replace("\"", "");
                    }
                }

                if (media.Id == 0)
                {
                    try
                    {
                        mediaInfo.UploadDate = DateTime.Now;
                        flag = IMedia.AddMedia(mediaInfo);

                    }
                    catch (Exception e)
                    {
                        //throw;
                    }

                }
                else
                {
                    flag = IMedia.UpdateMedia(mediaInfo);
                }
            }

            return Json(flag);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Save(MediaVM media)
        {
            bool flag = false;
            if (ModelState.IsValid)
            {
                var mapper = ObjectMapperManager.DefaultInstance.GetMapper<MediaVM, MediaInfo>();

                var mediaInfo = mapper.Map(media);
                mediaInfo.CompanyId = Sessions.User.CompanyId;
                if (!string.IsNullOrEmpty(media.MediaHtml))
                {
                    string pat = @"<embed\s+[^>]*\s*src\s*=\s*([']?)(?<url>\S+)'?[^>]*>";

                    Regex reg = new Regex(pat, RegexOptions.IgnoreCase);
                    Match m = reg.Match(media.MediaHtml);
                    if (m.Success)
                    {
                        mediaInfo.MediaUrl = m.Groups[2].Value.Replace("\"", "");
                    }
                }

                if (media.Id == 0)
                {
                    try
                    {
                        mediaInfo.UploadDate = DateTime.Now;
                        flag = IMedia.AddMedia(mediaInfo);

                    }
                    catch (Exception e)
                    {
                        //throw;
                    }

                }
                else
                {
                    flag = IMedia.UpdateMedia(mediaInfo);
                }
            }

            return Json(flag);
        }



        /// <summary>
        /// 删除相册
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Delete(string ids)
        {
            var b = false;
            if (!string.IsNullOrEmpty(ids))
            {
                var idList = ids.Split(',');
                var list = new List<int>();
                foreach (var s in idList)
                {
                    list.Add(Convert.ToInt32(s));
                }
                b = IMedia.DeleteMedia(list);
            }
            return Json(b);
        }

    }
}
